package com.baymax.test.scene.demo

import android.R.attr.thumb
import android.graphics.Color
import android.graphics.Rect
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.SeekBar
import com.baymax.lib.logger.KLog
import com.baymax.test.scene.BaseScene
import com.baymax.test.scene.Player
import com.baymax.test.scene.R
import com.baymax.test.scene.addButton
import com.baymax.test.scene.addDFView
import com.baymax.test.scene.newSceneFlexContainer
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.CustomViewTarget
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.target.Target
import com.bumptech.glide.request.transition.Transition
import com.facebook.drawee.drawable.ForwardingDrawable
import com.google.android.flexbox.FlexboxLayout
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.io.File

class SeekBarScene : BaseScene() {

    override fun onCreateView(p0: LayoutInflater, p1: ViewGroup, p2: Bundle?): View {
        return p1.newSceneFlexContainer() {
            val seekBar = SeekBar(p1.context)
            addDFView(1200, 300, seekBar) {
                seekBar.max = 10000
                seekBar.progress = 1000
//                seekBar.background = ColorDrawable(Color.TRANSPARENT)
                seekBar.isPressed = true
                thumb = ColorDrawable(Color.TRANSPARENT)
                this.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
                    override fun onProgressChanged(
                        seekBar: SeekBar?,
                        progress: Int,
                        fromUser: Boolean
                    ) {
                    }

                    override fun onStartTrackingTouch(seekBar: SeekBar?) {
                    }

                    override fun onStopTrackingTouch(seekBar: SeekBar?) {
                    }
                })
            }
            launch {
                while (true) {
                    seekBar.progress += 5
                    delay(1000)
                }
            }

            addButton("setThumb null") {
                seekBar.thumb = null
            }
            val fr = FrameLayout(context)
            val sur = SurfaceView(context)
            fr.addView(sur)
            fr.addView(Player(context).apply {
                start("http://ad.vod01.icntvcdn.com/media/new/2013/icntv2/media/2019/05/14/H142108PhilipsOHCWilliamOutputProdu15sTM.ts")
            })
            addDFView(100, 100, fr) {
                sur.holder.addCallback(object : SurfaceHolder.Callback {
                    override fun surfaceCreated(holder: SurfaceHolder) {
                        val c = holder.lockCanvas()
                        c.drawColor(Color.RED)
                        holder.unlockCanvasAndPost(c)
                    }

                    override fun surfaceChanged(
                        holder: SurfaceHolder,
                        format: Int,
                        width: Int,
                        height: Int
                    ) {
                    }

                    override fun surfaceDestroyed(holder: SurfaceHolder) {
                    }

                })
            }
            launch {
                repeat(10) { d ->
                    val png = if (d % 2 == 0) "progress-pause.png" else "progress-play.png"
                    loadSeekBarThumb(seekBar, png)
                    delay(10)
                }
            }
        }

    }

    var t: Target<*>? = null
    private fun FlexboxLayout.loadSeekBarThumb(seekBar: SeekBar, png: String) {
        Glide.with(seekBar).clear(t)
        val options = RequestOptions()
        options.override(60, 60);
        options.placeholder(ColorDrawable(Color.RED));
        val uri = "file://${File(context.cacheDir, png).absolutePath}"
        KLog.d("seekbar", "uri $uri")
         t = Glide.with(this).load(Uri.parse(uri)).apply(options)
            .into(object : SimpleTarget<Drawable>() {
                //                    override fun onResourceCleared(placeholder: Drawable?) {
                //                        seekBar.thumb = placeholder
                //                    }

                override fun onLoadFailed(errorDrawable: Drawable?) {
                    seekBar.thumb = errorDrawable

                }

                override fun onResourceReady(
                    resource: Drawable,
                    transition: Transition<in Drawable>?
                ) {

                    KLog.d("seekbar", "bound1 ${resource.bounds}")
                    seekBar.thumb = object : ForwardingDrawable(resource) {
                        override fun onBoundsChange(bounds: Rect) {
                            super.onBoundsChange(bounds)
                            KLog.d("seekbar", "bound2 $bounds")
                        }
                    }

                }

            })
    }
}